package com.jhl.seckill.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jhl.seckill.entity.User;
import com.jhl.seckill.mapper.UserMapper;
import com.jhl.seckill.service.UserService;
import com.jhl.seckill.utils.CookieUtil;
import com.jhl.seckill.utils.JWTUtil;
import com.jhl.seckill.utils.MD5Util;
import com.jhl.seckill.vo.LoginVo;
import com.jhl.seckill.vo.RespBean;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.UUID;


/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author JHL
 * @since 2022-04-28
 */
@Slf4j
@Service
@Primary

public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    UserMapper UserMapper;

    @Autowired
    RedisTemplate redisTemplate;

    @Override
    public RespBean doLogin(LoginVo loginVo, HttpServletRequest request, HttpServletResponse response) {
        String username = loginVo.getUsername();
        String password = loginVo.getPassword();


        log.info("前台用户名为:" + username);
        log.info("前台密码为:" + password);

        /**
         * 之前的想法是先查找redis是否存在这个用户：user:uuid，但是不行
         * 如果cookie被截获，那么黑客可以直接登录
         * 因此用户名密码判断是必须的
         */
//        可以先从缓存获取user，不成功再从数据库查询
//        User user = redisTemplate.opsForValue().get("user:" + uuid);
        User user = UserMapper.selectOne(new QueryWrapper<User>().eq("username", username));
        log.info("查找到的用户为:" + user);
        if (user == null) {
//            数据库查找不到此用户
            return RespBean.userNotExistsError(null);
        } else if (!user.getPassword().equals(MD5Util.inputPassToDBPass(password, user.getSalt()))) {
//            存在此用户但是密码不对
            return RespBean.passwordError(null);
        }


        String token = JWTUtil.createJWT(UUID.randomUUID().toString(), username, null);
//        用户信息存入redis
//        log.info("用户信息暂存redis，格式=user:uuid");
        redisTemplate.opsForValue().set("user:" + token, user);
        log.info("生成的token=" + token);

        log.info("doLogin执行完毕，返回数据");
        response.setHeader("token", token);
        return RespBean.success(token);

    }

    @Override
    public User geUserByCookie(String uuid, HttpServletRequest request, HttpServletResponse response) {
        if (StringUtils.isEmpty(uuid)) {
            return null;
        }
        log.info("尝试从redis获取缓存信息");
        User user = (User) redisTemplate.opsForValue().get("user:" + uuid);
//        System.out.println("geUserByCookie获得的user="+user);
        if (user != null) {
            CookieUtil.setCookie(request, response, "uuid", uuid);
        }
        return user;
    }
}
